package util

import (
	"encoding/json"
	"testing"

	"github.com/go-playground/assert/v2"
)

type testStruct struct {
	Hi interface{} `json:"hi,string"`
}

func TestGetIntOrString(t *testing.T) {
	//test by json
	jsonStr := []byte(`{"hi" : "123"}`)
	jsonInt := []byte(`{"hi" : 123}`)
	var p1 testStruct
	var p2 testStruct
	json.Unmarshal(jsonStr, &p1)
	json.Unmarshal(jsonInt, &p2)

	hi1, _ := GetIntOrString(p1.Hi)
	hi2, _ := GetIntOrString(p2.Hi)
	assert.Equal(t, hi1, hi2)
	assert.Equal(t, hi1, "123")
	assert.Equal(t, hi2, "123")

	var p3 testStruct
	jsonFloat := []byte(`{"hi" : 35.94}`)
	json.Unmarshal(jsonFloat, &p3)
	hi3, _ := GetIntOrString(p3.Hi)
	//auto convert to int
	assert.Equal(t, hi3, "35")

}
